import http from '../../services/http';

const state = {
	menuList: {},
	serveDetail:{},
};

const mutations = {
	setMenuList(state, payload) {
		state.menuList = payload.menuList;
	},
	setServeDetail(state, payload) {
		state.serveDetail = {
			...payload.serveDetail,
			...JSON.parse(payload.serveDetail.detailStr),
		};
		console.log({...payload.serveDetail,
			...JSON.parse(payload.serveDetail.detailStr),});
	},
};

const actions = {
	
	getServeDetail({ commit }, payload) {
		const {serveId} = payload;
		return new Promise((resolve, reject) => {
			http.get(`isp/api/v1/public/service/{${serveId}}`,{
				'id' : serveId,
			},redata => {
				if(redata.success){
					commit({
						type: 'setServeDetail',
						serveDetail: redata.data
					});
					resolve(redata.data);
				}else{
					reject('获取服务菜单列表--失败');
				}
			})
		})
	},
	
	getMenuList({ commit }, payload) {
		return new Promise((resolve, reject) => {
			http.get("/isp/api/v1/public/servCat/list",{
				page:0,
				size:100
			}, (fdata) => {
				if(fdata.success) {
					http.get("/isp/api/v1/public/service/serviceTree", (cdata) => {
						if(cdata.success) {
							let menuList = {};
							fdata.data.content.forEach(item=>{
								menuList[item.id] = {
									id:item.id,
									name:item.name,
									sub:[]
								}
							})
							cdata.data.forEach(citem=>{
								if(menuList[citem.catId]){
									menuList[citem.catId].sub.push(citem);
								}
							});
							commit({
								type: 'setMenuList',
								menuList: menuList
							});
							resolve(menuList);
						} else {
							reject('获取服务菜单--失败');
						}
					})
				} else {
					reject('获取服务菜单列表--失败')
				}
			})
		})
	},

};

export default {
	state,
	mutations,
	actions
};